package com.cyf.db.mapper;

import com.cyf.db.bean.MerchantInfo;
import com.cyf.db.bean.RechargeRecord;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商户信息
 * Created by cage on 2017/3/29.
 */

public interface MerchantInfoMapper {

    // 根据登录名、密码查询。可能多个
    @Select("select * from local_merchant_info where loginName = #{loginName} and passWord = #{passWord} and status = 1 and statusbit = 1")
    List<MerchantInfo> getByNameAndPwd(@Param("loginName") String loginName, @Param("passWord") String passWord);

    // 根据登录名、密码查询。可能多个
    @Select("<script>" +
            " select * from local_merchant_info where loginName = #{loginName}  and status = 1 and statusbit = 1 " +
            " <if test=\"parkingId != -1 \">" +
            "  and parkingId = #{parkingId} " +
            " </if>" +
            "</script>")
    List<MerchantInfo> getByName(@Param("loginName") String loginName, @Param("parkingId") Integer parkingId);

    @Select("select * from local_merchant_info where id = #{id} and statusbit = 1")
    MerchantInfo getById(@Param("id") Integer id);

    @Update("update local_merchant_info set remainAllHours = #{fee},ifUpload = 0 where id = #{id}")
    void updateMerchantInfoFreeHours(@Param("id") Integer id, @Param("fee") Integer fee);

    @Update("update local_merchant_info set balance = #{fee},ifUpload = 0  where id = #{id}")
    void updateMerchantInfoFreeAmount(@Param("id") Integer id, @Param("fee") Integer fee);


    //验证旧密码是否正确
    @Select("select id from local_merchant_info where loginName=#{loginName } and passWord=#{passWord }")
    List<Integer> getPassWord(@Param("loginName") String loginName, @Param("passWord") String passWord);

    //根据登录名更新密码
    @Update("update local_merchant_info set passWord=#{passWord } where id=#{id } ")
    void upatePwd(@Param("id") Integer id, @Param("passWord") String passWord);

    @Select("select * from local_merchant_info where merchantUUID=#{uuid}")
    MerchantInfo getByUUid(@Param("uuid") String uuid);

    @Select("select lr.*,lmi.stallPhone from local_rechargerecord lr " +
            "left join local_merchant_info lmi on lmi.localMerchantInfoId = lr.merchantId and lmi.parkingId = lr.merchantId " +
            "where lr.parkingId = #{parkingId} and lr.merchantId = #{merchantId}")
    List<RechargeRecord> selectByMerchat(@Param("parkingId") Integer parkingId, @Param("merchantId") Integer merchantId);

    @Update("update local_merchant_info set merchantUUID=#{uuid } where id=#{merchantId }")
    void updateQRCode(@Param("merchantId") Integer merchantId, @Param("uuid") String uuid);

    /**
     * 变更登录状态
     *
     * @param id
     * @param state
     */
    @Update("update local_merchant_info set loginState = #{state} where id = #{id} ")
    void updateMerchantInfoLoginState(@Param("id") Integer id, @Param("state") Integer state);

}
